package com.milotnt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.milotnt.form.MemberQueryForm;
import com.milotnt.mapper.ClassOrderMapper;
import com.milotnt.mapper.ClassTableMapper;
import com.milotnt.mapper.MemberMapper;
import com.milotnt.pojo.ClassOrderEntity;
import com.milotnt.pojo.ClassTableEntity;
import com.milotnt.pojo.MemberEntity;
import com.milotnt.service.ClassOrderService;
import com.milotnt.vo.ClassOrderVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
@Transactional
public class ClassOrderServiceImpl extends ServiceImpl<ClassOrderMapper, ClassOrderEntity> implements ClassOrderService {

    @Autowired
    private ClassOrderMapper classOrderMapper;

    @Autowired
    private ClassTableMapper classTableMapper;

    @Autowired
    private MemberMapper memberMapper;

    @Override
    public List<ClassOrderEntity> findAll() {
        LambdaQueryWrapper<ClassOrderEntity> queryWrapper = new LambdaQueryWrapper<>();
        return classOrderMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional
    public Boolean insertClassOrder(ClassOrderEntity classOrder) {
        Date date = new Date();
        classOrder.setCreateTime(date);
        ClassTableEntity classTable = classTableMapper.selectById(classOrder.getClassId());

        if (classTable == null){
            throw new RuntimeException("课程不存在");
        }

        MemberEntity member = memberMapper.selectOne(
                new LambdaQueryWrapper<MemberEntity>()
                        .eq(MemberEntity::getMemberAccount, classOrder.getMemberAccount())
                        .eq(MemberEntity::getMemberName, classOrder.getMemberName())
        );

        if (member == null){
            throw new RuntimeException("会员不存在");
        }

        if (member.getCardNextClass() < classTable.getClassTime()){
            throw new RuntimeException("会员课时余额不足");
        }
        member.setCardNextClass(member.getCardNextClass() - classTable.getClassTime());

        boolean updateResult = memberMapper.updateMemberByMemberAccount(member);
        boolean insertResult = classOrderMapper.insertClassOrder(classOrder);
        return updateResult && insertResult;
    }

    @Override
    public List<ClassOrderVo> selectClassOrderByMember(MemberQueryForm form) {
        if (form == null){
            throw new RuntimeException("查询参数不能为空");
        }
        if (form.getMemberName() == null && form.getMemberAccount() == null){
            throw new RuntimeException("查询参数内部值不能为空");
        }
        return classOrderMapper.selectclassOrderList(form);
    }

    @Override
    public Boolean deleteByClassOrderId(Long classOrderId) {
        ClassOrderEntity classOrder = classOrderMapper.selectById(classOrderId);

        if (classOrder == null){
            throw new RuntimeException("未报名该课程");
        }

        ClassTableEntity classTable = classTableMapper.selectById(classOrder.getClassId());

        if (classTable == null){
            throw new RuntimeException("课程不存在");
        }

        MemberEntity member = memberMapper.selectOne(new LambdaQueryWrapper<MemberEntity>()
                .eq(MemberEntity::getMemberAccount, classOrder.getMemberAccount())
                .eq(MemberEntity::getMemberName, classOrder.getMemberName()));

        if (member == null){
            throw new RuntimeException("会员不存在");
        }

        member.setCardNextClass(member.getCardNextClass() + classTable.getClassTime());
        Boolean updateResult = memberMapper.updateMemberByMemberAccount(member);
        Boolean deleteResult = classOrderMapper.deleteByClassOrderId(classOrderId);

        return updateResult && deleteResult;
    }

    @Override
    public ClassOrderEntity selectMemberByClassIdAndMemberAccount(ClassOrderEntity classOrder) {
        LambdaQueryWrapper<ClassOrderEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ClassOrderEntity::getClassId, classOrder.getClassId());
        wrapper.eq(ClassOrderEntity::getMemberAccount, classOrder.getMemberAccount());
        return classOrderMapper.selectOne(wrapper);
    }

    @Override
    public List<ClassOrderVo> selectMemberOrderList(Long classId) {
        return classOrderMapper.selectMemberOrderList(classId);
    }
}
